<template>
    <el-dialog :title="$t('game.other.dictionary.dialog.create')" width="460px" :visible.sync="dialogVisible" @close="$emit('update:visible', false)">
        <div class="pr-12">
            <el-form :model="dataCache" label-width="85px">
                <el-form-item :label="$t('game.other.dictionary.classify')" :error="Boolean(msg.errors['classify']) ? msg.errors['classify'][0] : ''" required>
                    <el-select v-model="dataCache.classify" class="block">
                        <el-option v-for="item in $t('game.other.dictionary.classifyList')"
                                   :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('game.setting.language.name')" :error="Boolean(msg.errors['language']) ? msg.errors['language'][0] : ''" required>
                    <el-select v-model="dataCache.language" class="block">
                        <el-option v-for="item in $t('game.setting.language.languages')"
                                   :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('game.other.dictionary.content')" required
                              :error="Boolean(msg.errors['content']) ? msg.errors['content'][0] : '' || msg.code == 44200 ? msg.message : ''">
                    <el-input v-model="dataCache.content" type="textarea" :autosize="{ minRows: 5, maxRows: 7}"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">{{ $t('action.save') }}</el-button>
                    <el-button @click="dialogVisible = false">{{ $t('action.cancel') }}</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: "ItemCreate",
    props: ['visible'],
    data() {
        return {
            dialogVisible: false,
            dataCache: {
                id: null,
                content: '',
                language: '',
                classify: '',
            },
            msg: {
                code: 200,
                message: '',
                errors: {},
            }
        }
    },
    watch: {
        visible: function (n, o) {
            if (n) {
                // 初始化参数
                this.initData();
                // 显示模态框
                this.dialogVisible = n;
            }
        }
    },
    methods: {
        initMsg() {
            this.msg.code = 200;
            this.msg.message = '';
            this.msg.errors = {};
        },
        initData() {
            this.initMsg();
            this.dataCache.content = '';
            this.dataCache.language = '';
            this.dataCache.classify = '';
        },
        onSubmit() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/game/other/dictionary/store', this.dataCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.new')}),
                        showClose: true
                    });
                    this.dialogVisible = false;
                    // 广播事件到父组件
                    this.$emit('create');
                } else if (_.includes([42000, 44200], response.data.resp_msg.code)) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.new')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>

</style>
